#import "BWColorKeyArrayController.h"
#import "BWThread.h"
#import "BWAppController.h"


@implementation BWColorKeyArrayController



// --------------------------------------------------
// drag and droppage

- (NSArray *) draggedTypes
{
    NSArray *draggedTypes;
    draggedTypes = [NSArray arrayWithObjects: 
                                BWThread_CopiedRowsType,
                            BWSymbol_PackageNameType,
                            BWSymbol_SymbolIdType, nil];

    return (draggedTypes);

} // draggedTypes


- (void) awakeFromNib
{
    [tableview registerForDraggedTypes: [self draggedTypes]];

} // awakeFromNib


- (NSDragOperation) tableView: (NSTableView *) view
                 validateDrop: (id <NSDraggingInfo>) info
                  proposedRow: (int) row
        proposedDropOperation: (NSTableViewDropOperation) op
{
    [view setDropRow: row
          dropOperation: op];

    NSDragOperation dragOp = NSDragOperationCopy;

    return (dragOp);

} // validateDrop


- (void) addDroppedColor: (BWThreadColor *) color
                   atRow: (int) row
{
    BWThread *thread;
    thread = [[BWThread alloc] init];
    [thread autorelease];
        
    BWThreadColor *copy = [[color copy] autorelease];

    [thread setColor: copy];

    [self insertObject: thread
          atArrangedObjectIndex: row];

} // addDroppedColor


- (void) addDroppedSymbol: (BWSymbol *) symbol
                   atRow: (int) row
{
    BWThread *thread;
    thread = [[BWThread alloc] init];
    [thread autorelease];
        
    [thread setSymbol: symbol];

    [self insertObject: thread
          atArrangedObjectIndex: row];

} // addDroppedSymbol


- (void) changeToDroppedColor: (BWThreadColor *) color
                        atRow: (int) row
{
    NSArray *arrangedObjects = [self arrangedObjects];
    BWThread *thread;
    thread = [arrangedObjects objectAtIndex: row];
    [[thread retain] autorelease];

    // !!! if I don't remove it first, it crashes.  I don't know why.
    [self removeObjectAtArrangedObjectIndex: row];

    BWThreadColor *copy;
    copy = [[color copy] autorelease];

    [thread setColor: copy];

    [self insertObject: thread  atArrangedObjectIndex: row];

} // changeToDroppedColor


- (void) changeToDroppedSymbol: (BWSymbol *) symbol
                        atRow: (int) row
{
    NSArray *arrangedObjects = [self arrangedObjects];
    BWThread *thread;
    thread = [arrangedObjects objectAtIndex: row];
    [[thread retain] autorelease];

    // !!! if I don't remove it first, it crashes.  I don't know why.
    [self removeObjectAtArrangedObjectIndex: row];

    [thread setSymbol: symbol];

    [self insertObject: thread  atArrangedObjectIndex: row];

} // changeToDroppedSymbol


- (BOOL) tableView: (NSTableView *) view
        acceptDrop: (id <NSDraggingInfo>) info
               row: (int) row
     dropOperation: (NSTableViewDropOperation) op
{
    BWThreadColor *color = nil;
    BWSymbol *symbol = nil;


    NSData *data;
    data = [[info draggingPasteboard]
               dataForType: BWThread_CopiedRowsType];
    if (data != nil) {
    
        NSArray *array;
        array = [NSUnarchiver unarchiveObjectWithData: data];
        
        color = [array objectAtIndex: 0];

    } else {
        NSString *packageName;
        packageName = [[info draggingPasteboard]
                          propertyListForType: BWSymbol_PackageNameType];

        NSNumber *symbolId;
        symbolId = [[info draggingPasteboard]
                       propertyListForType: BWSymbol_SymbolIdType];

        symbol = [[BWAppController globalController]
                     symbolForPackageName: packageName
                     symbolId: [symbolId intValue]];
    }

    if (op == NSTableViewDropOn) {
        // replace existing

        if (color != nil) {
            [self changeToDroppedColor: color
                  atRow: row];
        } else if (symbol != nil) {
            [self changeToDroppedSymbol: symbol
                  atRow: row];
        }

    } else if (op == NSTableViewDropAbove) {
        // add new threads based on the dragged-in colors
        if (color != nil) {
            [self addDroppedColor: color
                  atRow: row];
        } else if (symbol != nil) {
            [self addDroppedSymbol: symbol
                  atRow: row];
        }

    } else {
        NSLog (@"unexpected operation (%d) in %s",
               op, __FUNCTION__);
    }

done:
    // documentation doesn't actually say what this signifies
    return (YES);

} // acceptDrop


@end // BWColorKeyArrayController

